////////////////////////////////////////////////////////////////////////////////
//(c) Intelliquip, Inc. 2001
////////////////////////////////////////////////////////////////////////////////
//$Log:
// 2    Version 10.0 (Current View)1.1         6/1/2011 2:03:59 PM    Scott
//      Frees     IDD 09911:  xSheets and IQX integration
// 1    Version 10.0 (Current View)1.0         5/19/2011 2:22:46 PM   Jeff
//      Heigl      
//$
//$NoKeywords$
////////////////////////////////////////////////////////////////////////////////
package com.intelliquip.hydraulicselection.control.web;

import com.intelliquip.core.Attributes;
import com.intelliquip.hydraulicselection.OperatingConditions;
import com.intelliquip.hydraulicselection.PumpDetails;
import com.intelliquip.hydraulicselection.RejectStatus;
import com.intelliquip.hydraulicselection.SelectorPerformanceOption;
import com.intelliquip.hydraulicselection.search.SearchParametersSinglePump;
import com.intelliquip.performanceoptions.SubcategoryConstants;

public class RedirectConfigForPump extends RedirectConfig {

	public RedirectConfigForPump(RedirectParams parameterObject) {
		super(parameterObject);
	}

	@Override
	protected void searchAndValidate() throws RedirectValidationException {
		PumpDetails pumpDetails = pumpSearch();

		validatePumpDetails(pumpDetails);
		validatePumpTypeCan(pumpDetails);
	}

	private PumpDetails pumpSearch() {
		PumpDetails pumpDetails = userPreferences.getSelectedPumpDetails();
		try {
			OperatingConditions oc = userPreferences.getOperatingConditions();
			SearchParametersSinglePump params = new SearchParametersSinglePump(userPreferences);
			pumpDetails = productLineWeb.getPumpDetails(params);
			pumpDetails.setUserSettings(userSettings);
			if(oc.isProductLineExists()) {
				userPreferences.setSelectedPumpDetails(pumpDetails);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
		}
		return pumpDetails;
	}

	private void validatePumpDetails(PumpDetails pumpDetails) throws RedirectValidationException {
		if(pumpDetails == null || pumpDetails.getRejectStatus() == RejectStatus.FULL_REJECT) {
			String targetReject = targetPath + ScreenNames.PERFORMANCE_DATASHEET_URL;
			throw new RedirectValidationException(getFullRejectMessage(), targetReject);
		}
	}

	/**test for barrel pump NPSH compatibility*/
	private void validatePumpTypeCan(PumpDetails pumpDetails) throws RedirectValidationException {
		SelectorPerformanceOption spo = getPerformanceOptionPumpType(pumpDetails);
		if(spo != null) {
			boolean configWarning2 = spo.getCode().equals("PpType-Can") && pumpDetails.getDoubleValue(Attributes.NPSHR_RATED_DISPLAY) == -9999;
			if(configWarning2) {
				String label = pageLabels.getLabel("configWarning2");
				throw new RedirectValidationException(label, getCurrentScreenUrl());
			}
		}
	}

	private SelectorPerformanceOption getPerformanceOptionPumpType(PumpDetails pumpDetails) {
		SelectorPerformanceOption spo = null;
		try {
			spo = pumpDetails.getPerformanceOptionSelected(SubcategoryConstants.PUMP_TYPE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spo;
	}

}
